package com.zl.learning.db;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Test3 {
    static class User {
        private Integer id;
        private String name;
        private Integer age;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }
    }

    public Connection getConn() {
        try {
            return DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true", "root", "*Jsnjcc*");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Test
    public void test1() {
        String sql = "INSERT INTO `test`.`user` (`name`, `age`) VALUES (?,?)";
        User user = new User();
//        user.setName("zhangsan");
//        user.setAge(11);
        try (Connection conn = getConn();
             PreparedStatement pstmt = conn.prepareStatement(sql);) {
            int i = 0;
            pstmt.setString(++i, user.getName());
            pstmt.setObject(++i, user.getAge());
            pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
